package com.example.common;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import com.example.vo.dataserie;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

public class AsyncPostData extends AsyncTask<String, Integer, String> {
	private Context mContext;

	public AsyncPostData(Context ctx) {
		mContext = ctx;
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
	}

	HttpResponse httpResponse;

	private void postData(String url) {
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		String json = "";
		try {
			// dataRecord.setNickname("dodung");
			// dataRecord.setPassword("password");
			dataserie _data = new dataserie();
			_data.set_user(4);
			_data.set_device(1);
			JSONObject jsonObject = new JSONObject();
//			jsonObject.accumulate("_user", "user8");
//			jsonObject.accumulate("_dataSeries", _data);

			json = jsonObject.toString();
			StringEntity se = new StringEntity(json);
			httpPost.setEntity(se);
			httpPost.setHeader("Accept", "application/json");
			httpPost.setHeader("Content-type", "application/json");
			httpResponse = httpClient.execute(httpPost);

		} catch (Exception e) {
		}
	}

	@Override
	protected String doInBackground(String... params) {
		// TODO Auto-generated method stub
		postData(params[0]);
		return null;
	}

	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		Toast.makeText(mContext, httpResponse.toString(), 1000).show();
		super.onPostExecute(result);
	}
}
